package com.lc.japaneseapp;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;

import com.example.japaneseapp.NewWordActivity;
import com.example.japaneseapp.R;
import com.lc.DetailCharacter.DetailGridView;
import com.lc.adapter.CharacterAdapter;

public class ViewCharacterActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		private CharacterAdapter adapterCharacter;
		private GridView gridviewOfCharacter;
		private DetailGridView detailCharacter;
		private Button btnChangGridTypeChar, btnTest, btnNewWord;
		private boolean isHiragana = true;

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {

			// display gridView Hiragana or Katakana
			/*
			 * sau day minh o the tach. ra 2 activity voi 2 function em de vao
			 * trong nay de anh test cho nhanh function gird view kia em lam
			 * xong roi con phan audio thoi dang nghi cach cho no do thu cong
			 * kieu nhu an vao cai nao thi no tim den R.raw de tuong ung ay thu
			 * cong nhat la so sanh string xong de chon audio tuong ung nhung ko
			 * hieu nang lam
			 */
			final View rootView = inflater.inflate(
					R.layout.fragment_gridview_character_display, container,
					false);
			btnChangGridTypeChar = (Button) rootView
					.findViewById(R.id.btnChangeTypeChar);
			btnTest = (Button) rootView.findViewById(R.id.btnTest);
			btnChangGridTypeChar.setText("あ");
			btnNewWord = (Button) rootView.findViewById(R.id.btnNewWord);
			// make and set a adapter to grid view
			adapterCharacter = new CharacterAdapter(getActivity());
			adapterCharacter.setCharacterApdapter(isHiragana);
			isHiragana = !isHiragana;
			gridviewOfCharacter = (GridView) rootView
					.findViewById(R.id.characterHiraganaGrid);
			gridviewOfCharacter.setAdapter(adapterCharacter);
			gridviewOfCharacter
					.setOnItemClickListener(new AdapterView.OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {

							detailCharacter = new DetailGridView(getActivity());
							detailCharacter.showDetail(position, !isHiragana);

						}
					});
			btnChangGridTypeChar.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (isHiragana) {
						btnChangGridTypeChar.setText("あ");
					} else {
						btnChangGridTypeChar.setText("ア");
					}
					adapterCharacter.setCharacterApdapter(isHiragana);
					gridviewOfCharacter = (GridView) rootView
							.findViewById(R.id.characterHiraganaGrid);
					gridviewOfCharacter.setAdapter(adapterCharacter);

					isHiragana = !isHiragana;
				}
			});
			super.onCreate(savedInstanceState);

			btnTest.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(getActivity(),
							TestActivity.class);
					startActivity(intent);
				}
			});

			btnNewWord.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getActivity(),
							NewWordActivity.class);
					startActivity(intent);
					getActivity().finish();

				}
			});
			return rootView;

		}

	}
}
